﻿package com.gengine.resource.core
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class StreamLoader extends EventDispatcher
    {
        protected var _url:String;
        private var _urlLoader:URLLoader;
        private var _contentType:String;
        private var _loaderInfo:LoaderInfo;
        private var _isNeedReload:Boolean = true;
        protected var _verifyTime:int;
        protected var _reloadTimes:int = 0;
        private var _maxReloadTimes:int = 5;
        private var _reloadSec:int = 5;
        protected var loadedByteArray:ByteArray;

        public function StreamLoader()
        {
            return;
        }// end function

        public function get isNeedReload() : Boolean
        {
            return this._isNeedReload;
        }// end function

        public function set isNeedReload(param1:Boolean) : void
        {
            this._isNeedReload = param1;
            return;
        }// end function

        public function get reloadSec() : int
        {
            return this._reloadSec;
        }// end function

        public function set reloadSec(param1:int) : void
        {
            this._reloadSec = param1;
            return;
        }// end function

        public function get maxReloadTimes() : int
        {
            return this._maxReloadTimes;
        }// end function

        public function set maxReloadTimes(param1:int) : void
        {
            this._maxReloadTimes = param1;
            return;
        }// end function

        protected function loadImp(param1:String) : void
        {
            this._url = param1;
            if (this._url)
            {
                if (this._urlLoader == null)
                {
                    this._urlLoader = new URLLoader();
                    this._urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                }
                this._urlLoader.addEventListener(Event.COMPLETE, this.onCompleteHandler);
                this._urlLoader.addEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
                this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
                this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onIOError);
                this._urlLoader.load(new URLRequest(this._url));
            }
            return;
        }// end function

        protected function removeLoadEvent(param1:Boolean) : void
        {
            this.clearVerifyTime();
            if (!this._urlLoader)
            {
                return;
            }
            this._urlLoader.removeEventListener(Event.COMPLETE, this.onCompleteHandler);
            this._urlLoader.removeEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
            this._urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
            this._urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onIOError);
            if (param1)
            {
                try
                {
                    this._urlLoader.close();
                }
                catch (e:Error)
                {
                }
            }
            return;
        }// end function

        protected function onCompleteHandler(event:Event) : void
        {
            this.removeLoadEvent(false);
            this.streamloadComplete(event.target.data);
            this.initialize(event.target.data);
            return;
        }// end function

        protected function streamloadComplete(param1) : void
        {
            return;
        }// end function

        protected function onProgressHandler(event:ProgressEvent) : void
        {
            dispatchEvent(event);
            if (event.bytesLoaded == event.bytesTotal)
            {
                this.clearVerifyTime();
                this._verifyTime = setTimeout(this.reload, 2000);
            }
            else if (this._isNeedReload)
            {
                this.verifyTime();
            }
            return;
        }// end function

        private function verifyTime() : void
        {
            this.clearVerifyTime();
            this._verifyTime = setTimeout(this.reload, this._reloadSec * 1000);
            return;
        }// end function

        private function clearVerifyTime() : void
        {
            if (this._verifyTime)
            {
                clearTimeout(this._verifyTime);
                this._verifyTime = 0;
            }
            return;
        }// end function

        private function reload() : void
        {
            var _loc_1:int = 0;
            var _loc_2:String = this;
            var _loc_3:* = this._reloadTimes + 1;
            _loc_2._reloadTimes = _loc_3;
            this.clearVerifyTime();
            this.stop();
            if (this._reloadTimes < this._maxReloadTimes)
            {
                Log.debugMsg(LogType.other, this._url + "重新加载" + this._reloadTimes + "次");
                _loc_1 = this._url.indexOf("?");
                if (_loc_1 >= 0)
                {
                    this._url = this._url + MathUitl.random(0, 9);
                }
                else
                {
                    this._url = this._url + ("?" + MathUitl.random(0, 9));
                }
                this.loadImp(this._url);
            }
            else
            {
                Log.debugMsg(LogType.other, this._url + "重新加载失败");
                this.onIOError(new ErrorEvent("重新加载失败"));
            }
            return;
        }// end function

        protected function logMessage(param1:String) : void
        {
            return;
        }// end function

        private function onIOError(event:ErrorEvent) : void
        {
            this.removeLoadEvent(false);
            this.onIOErrorHandler(event);
            return;
        }// end function

        protected function onIOErrorHandler(event:ErrorEvent) : void
        {
            return;
        }// end function

        public function initialize(param1) : void
        {
            if (!(param1 is ByteArray))
            {
                if (Global.isDebugModle)
                {
                    throw new Error("Default Resource can only process ByteArrays!");
                }
                this.loadedByteArray = null;
            }
            else
            {
                this.loadedByteArray = param1 as ByteArray;
            }
            return;
        }// end function

        public function loadByteArray() : void
        {
            var _loc_1:* = new Loader();
            if (this.loadedByteArray)
            {
                _loc_1.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadComplete);
                _loc_1.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onChangeIOErrorHandler);
                _loc_1.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onChangeIOErrorHandler);
                _loc_1.loadBytes(this.loadedByteArray);
                this.loadedByteArray = null;
            }
            else
            {
                this.onIOErrorHandler(null);
            }
            return;
        }// end function

        protected function onContentReady(param1) : Boolean
        {
            return true;
        }// end function

        private function onLoadComplete(event:Event) : void
        {
            var _loc_2:* = event.target as LoaderInfo;
            this._loaderInfo = _loc_2;
            _loc_2.removeEventListener(Event.COMPLETE, this.onLoadComplete);
            _loc_2.removeEventListener(IOErrorEvent.IO_ERROR, this.onChangeIOErrorHandler);
            _loc_2.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onChangeIOErrorHandler);
            if (this.onContentReady(event ? (event.target.content) : (null)))
            {
                this.onLoadCompleteHandler();
            }
            return;
        }// end function

        protected function onLoadCompleteHandler() : void
        {
            dispatchEvent(new Event(Event.COMPLETE));
            return;
        }// end function

        protected function onChangeIOErrorHandler(event:IOErrorEvent) : void
        {
            var _loc_2:* = event.target as LoaderInfo;
            _loc_2.removeEventListener(Event.COMPLETE, this.onLoadComplete);
            _loc_2.removeEventListener(IOErrorEvent.IO_ERROR, this.onChangeIOErrorHandler);
            _loc_2.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onChangeIOErrorHandler);
            this.onIOErrorHandler(event);
            return;
        }// end function

        public function dispose() : void
        {
            this.stop();
            this.clearVerifyTime();
            this._url = null;
            this._loaderInfo = null;
            this._reloadTimes = 0;
            this._reloadSec = 10;
            this._maxReloadTimes = 10;
            this._isNeedReload = true;
            return;
        }// end function

        public function get url() : String
        {
            return this._url;
        }// end function

        public function get contentType() : String
        {
            if (this._loaderInfo)
            {
                return this._loaderInfo.contentType;
            }
            return null;
        }// end function

        public function get loaderInfo() : LoaderInfo
        {
            return this._loaderInfo;
        }// end function

        public function stop() : void
        {
            if (this._urlLoader)
            {
                this._urlLoader.removeEventListener(Event.COMPLETE, this.onCompleteHandler);
                this._urlLoader.removeEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
                this._urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
                this._urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onIOError);
                try
                {
                    if (this._urlLoader.bytesLoaded != this._urlLoader.bytesTotal)
                    {
                        this._urlLoader.close();
                    }
                }
                catch (e:Error)
                {
                }
            }
            return;
        }// end function

    }
}
